//
//  ViewController.m
//  ScrollRunLoopDemo
//
//  Created by 刘洋 on 2018/8/3.
//  Copyright © 2018年 LSH. All rights reserved.
//
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//导航条高度 64或者84
#define  NAVBARSTATUSBARHEIGHT ([[UIApplication sharedApplication] statusBarFrame].size.height+44)

#import "ViewController.h"

#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *theVcArray;
@property ( nonatomic, strong) UIScrollView *mainscrollView;
@end

@implementation ViewController
- (NSMutableArray *)theVcArray {
    if (_theVcArray == nil) {
        _theVcArray = [NSMutableArray array];
    }
    return _theVcArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpSubviews];
}

- (void)setUpSubviews {
    
    UIViewController *tempOneVc = [[UIViewController alloc] init];
    
    OneViewController *oneVc = [[OneViewController alloc] init];
    TwoViewController *twoVc = [[TwoViewController alloc] init];
    ThreeViewController *threeVc = [[ThreeViewController alloc] init];
    
    UIViewController *tempLastVc = [[UIViewController alloc] init];
    
     [self.theVcArray addObjectsFromArray:@[tempOneVc,oneVc,twoVc,threeVc,tempLastVc]];
    
    _mainscrollView = [[UIScrollView alloc] init];
    _mainscrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVBARSTATUSBARHEIGHT);
    _mainscrollView.contentSize = CGSizeMake(self.theVcArray.count*SCREEN_WIDTH,SCREEN_HEIGHT - NAVBARSTATUSBARHEIGHT);
    _mainscrollView.backgroundColor = [UIColor clearColor];
    _mainscrollView.showsVerticalScrollIndicator = NO;
    _mainscrollView.showsHorizontalScrollIndicator = NO;
    _mainscrollView.delegate = self;
    _mainscrollView.bounces = NO;
    _mainscrollView.scrollEnabled = YES;
    _mainscrollView.userInteractionEnabled = YES;
    _mainscrollView.pagingEnabled = YES;
    _mainscrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view addSubview:_mainscrollView];
    
    for (int i = 0; i < self.theVcArray.count; i++) {
        UIViewController *theViewController = (UIViewController *)self.theVcArray[i];
        theViewController.view.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, self.mainscrollView.frame.size.height);
        [self.mainscrollView addSubview:theViewController.view];
        [self addChildViewController:theViewController];
    }
    
    self.mainscrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.mainscrollView)
    {
        NSLog(@"theContentOffset.x%f",self.mainscrollView.contentOffset.x);
        NSInteger p = self.mainscrollView.contentOffset.x/SCREEN_WIDTH;
        if (p == 0) {
            [self.mainscrollView setContentOffset:CGPointMake((self.theVcArray.count - 2) * SCREEN_WIDTH, 0) animated:NO];
        } else if (p == self.theVcArray.count - 1) {
            [self.mainscrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];
        }
        NSLog(@"当前是第%ld页",p);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
